﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class TankHealth : MonoBehaviour {

	public float hp = 100;

	public GameObject tankExplosion;
	public AudioClip tankExplosionAudio;

	public Slider hpSlider;

	private float maxHp;
	// Use this for initialization
	void Start () {
		maxHp = hp;
		hpSlider.interactable = false;//屏蔽鼠标操作响应
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	void TakeDamage(){
		if (hp <= 0)
			return;

		hp -= Random.Range(10,20);
		hpSlider.value = (float)hp / maxHp;

		if (hp <= 0) {
			AudioSource.PlayClipAtPoint(tankExplosionAudio, transform.position, 1);
			//血量为0，坦克爆炸死亡
			GameObject.Instantiate (tankExplosion, this.transform.position+Vector3.up, this.transform.rotation);
			
			GameObject.Destroy (this.gameObject);
		}
	}
}
